package com.uit.gis.ui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.SwingUtilities;
import javax.swing.border.TitledBorder;

import com.uit.gis.controller.MainController;
import com.uit.gis.util.Language;

public class PanelDatabase extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;

	private static final String BTN_CONNECTION = "BTN_CONNECTION";
	private static final String BTN_DISCONNECTION = "BTN_DISCONNECTION";
	private static final String CHBOX_SAVE = "CHBOX_SAVE";
	private JTextField textFieldHostname;
	private JTextField textFieldPort;
	private JTextField textFieldSID;
	private JTextField textFieldUserName;
	private JPasswordField textFieldPass;
	private JLabel lbConnectionStatus;
	JButton btnConnect;
	MainController controller;
	private JPanel panel;
	JCheckBox chckbxSaveOnThis;
	

	/**
	 * Create the panel.
	 */
	public PanelDatabase(MainController controller) {
		 
		this.controller = controller;
		SpringLayout springLayout = new SpringLayout();
		setLayout(springLayout);

		panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Login to Oracle server",
				TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setForeground(Color.WHITE);
		springLayout.putConstraint(SpringLayout.NORTH, panel, 38,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, panel, 25,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, panel, 361,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.EAST, panel, 502,
				SpringLayout.WEST, this);

		add(panel);

		textFieldHostname = new JTextField();
		textFieldHostname.setColumns(10);
		springLayout.putConstraint(SpringLayout.NORTH, textFieldHostname, 77,
				SpringLayout.NORTH, this);
		springLayout.putConstraint(SpringLayout.WEST, textFieldHostname, 392,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, textFieldHostname, -10,
				SpringLayout.EAST, this);

		textFieldPort = new JTextField();
		textFieldPort.setColumns(10);
		springLayout.putConstraint(SpringLayout.WEST, textFieldPort, 374,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, textFieldPort, -28,
				SpringLayout.EAST, this);

		textFieldUserName = new JTextField();
		textFieldUserName.setColumns(10);
		springLayout.putConstraint(SpringLayout.EAST, textFieldUserName, -188,
				SpringLayout.EAST, this);

		JLabel lbPort = new JLabel("Port");
		springLayout.putConstraint(SpringLayout.WEST, lbPort, 58,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH, textFieldPort, -3,
				SpringLayout.NORTH, lbPort);

		JLabel lblNewLabel_3 = new JLabel("Username");
		springLayout.putConstraint(SpringLayout.SOUTH, lbPort, -6,
				SpringLayout.NORTH, lblNewLabel_3);

		JLabel lblNewLabel_2 = new JLabel("SID");
		springLayout.putConstraint(SpringLayout.WEST, lblNewLabel_2, 61,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.WEST, textFieldUserName, 136,
				SpringLayout.EAST, lblNewLabel_2);
		springLayout.putConstraint(SpringLayout.NORTH, textFieldUserName, -3,
				SpringLayout.NORTH, lblNewLabel_2);

		JLabel lblPassword = new JLabel("Password");
		springLayout.putConstraint(SpringLayout.EAST, lblPassword, 126,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.WEST, lblNewLabel_3, 0,
				SpringLayout.WEST, lblPassword);
		springLayout.putConstraint(SpringLayout.SOUTH, lblNewLabel_3, -24,
				SpringLayout.NORTH, lblPassword);
		springLayout.putConstraint(SpringLayout.SOUTH, lblNewLabel_2, -6,
				SpringLayout.NORTH, lblPassword);
		JLabel lbHostName = new JLabel("Host name");
		springLayout.putConstraint(SpringLayout.WEST, lbHostName, 130,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.NORTH, lbHostName, 26,
				SpringLayout.SOUTH, panel);

		textFieldPass = new JPasswordField();
		springLayout.putConstraint(SpringLayout.WEST, textFieldPass, 47,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, textFieldPass, 203,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.WEST, lblPassword, 0,
				SpringLayout.WEST, textFieldPass);

		btnConnect = new JButton("Connect");

		btnConnect.addActionListener(this);
		btnConnect.setActionCommand(BTN_CONNECTION);
		
		springLayout.putConstraint(SpringLayout.WEST, btnConnect, 161,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, btnConnect, -43,
				SpringLayout.SOUTH, this);
		springLayout.putConstraint(SpringLayout.EAST, btnConnect, 293,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.SOUTH, textFieldPass, -24,
				SpringLayout.NORTH, btnConnect);

		textFieldSID = new JTextField();
		textFieldSID.setColumns(10);
		springLayout.putConstraint(SpringLayout.WEST, textFieldSID, 318,
				SpringLayout.WEST, this);
		springLayout.putConstraint(SpringLayout.EAST, textFieldSID, -84,
				SpringLayout.EAST, this);
		springLayout.putConstraint(SpringLayout.NORTH, textFieldSID, 53,
				SpringLayout.SOUTH, panel);
		
		chckbxSaveOnThis = new JCheckBox("Save on this computer");
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.TRAILING)
				.addGroup(Alignment.LEADING, gl_panel.createSequentialGroup()
					.addGap(49)
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(lbPort)
						.addComponent(lbHostName)
						.addComponent(lblNewLabel_2)
						.addComponent(lblNewLabel_3)
						.addComponent(lblPassword, GroupLayout.PREFERRED_SIZE, 79, GroupLayout.PREFERRED_SIZE))
					.addGap(19)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(textFieldHostname, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldPort, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldSID, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldUserName, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)
						.addComponent(textFieldPass, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(162, Short.MAX_VALUE))
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(chckbxSaveOnThis)
					.addGap(18)
					.addComponent(btnConnect, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE)
					.addGap(38))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(19)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldHostname, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lbHostName))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(textFieldPort, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lbPort))
					.addGap(21)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldSID, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_2))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(textFieldUserName, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNewLabel_3))
					.addGap(18)
					.addGroup(gl_panel.createParallelGroup(Alignment.TRAILING)
						.addComponent(textFieldPass, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblPassword))
					.addGap(37)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnConnect)
						.addComponent(chckbxSaveOnThis))
					.addGap(46))
		);
		panel.setLayout(gl_panel);

		lbConnectionStatus = new JLabel("Disconnect");
		springLayout.putConstraint(SpringLayout.SOUTH, lbConnectionStatus, -29,
				SpringLayout.SOUTH, this);
		springLayout.putConstraint(SpringLayout.EAST, lbConnectionStatus, -35,
				SpringLayout.EAST, this);
		add(lbConnectionStatus);
		lbConnectionStatus.setText(Language.STATUS_DICONNECTED);
		springLayout.putConstraint(SpringLayout.NORTH, lblPassword, 0,
				SpringLayout.NORTH, lbConnectionStatus);
		chckbxSaveOnThis.addActionListener(this);
		chckbxSaveOnThis.setActionCommand(CHBOX_SAVE); 
		//Load & init configuration settings
		loadConfigurationValues();
	}
	protected void loadConfigurationValues() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				chckbxSaveOnThis.setSelected(controller.databaseConfig.isSaveOnComputer());
				if(controller.databaseConfig.isSaveOnComputer())
				{
					textFieldHostname.setText(controller.databaseConfig.getHostName());
					textFieldPort.setText("1521");
					textFieldSID.setText(controller.databaseConfig.getsId());
					textFieldUserName.setText(controller.databaseConfig.getUserName());
					textFieldPass.setText(controller.databaseConfig.getPassWord());
				}
			}
		});
		
	}

	public void updateConnectionGUI(final boolean connected) {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				textFieldHostname.setEnabled(!connected);
				textFieldPort.setEnabled(!connected);
				textFieldSID.setEnabled(!connected);
				textFieldUserName.setEnabled(!connected);
				textFieldPass.setEnabled(!connected);
				if (connected) {
					btnConnect.setActionCommand(BTN_DISCONNECTION);
					btnConnect.setText(Language.STATUS_DISCONNECT);
					lbConnectionStatus.setText(Language.STATUS_CONNECTED);
				} else {
					lbConnectionStatus.setText(Language.STATUS_DICONNECTED);
					btnConnect.setActionCommand(BTN_CONNECTION);
					btnConnect.setText(Language.STATUS_CONNECT);
				}
			}
		});

	}

	public void saveConfigFile()
	{
		try{
			   controller.databaseConfig.setSaveOnComputer(chckbxSaveOnThis.isSelected());
			   controller.databaseConfig.setHostName(textFieldHostname.getText());
			   controller.databaseConfig.setPort(textFieldPort.getText());
			   controller.databaseConfig.setsId(textFieldSID.getText());
			   controller.databaseConfig.setUserName(textFieldUserName.getText());
			   controller.databaseConfig.setPassword(new String(textFieldPass.getPassword()));  
			   controller.databaseConfig.writeConfigFile();
			    
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getActionCommand()) {
		case BTN_CONNECTION: {
			final String host = textFieldHostname.getText();
			final String port = textFieldPort.getText();
			final String sId = textFieldSID.getText();
			final String username = textFieldUserName.getText();
			final String password = new String(textFieldPass.getPassword());
			
			Thread connectThread = new Thread(new Runnable() { 
				@Override
				public void run() {
					// TODO Auto-generated method stub
					controller.openDatabase(host, port, sId, username,password); 
					saveConfigFile();
				}
			});
			connectThread.start();

		}
			break;
		case BTN_DISCONNECTION: {
			Thread disconnectThread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					controller.closeDatabase(); 
					controller.updateDatabaseGUI(false);
					//saveConfigFile();
				}
			});
			disconnectThread.start();
		}
			break;
		case CHBOX_SAVE:
		{  
			saveConfigFile();
		   
		}
		break;
		default:
			break;
		}
	}
}
